OE_Next Class_Interface
OpenEuphoria Next
See also https://openeuphoria.org/forum/135932.wc
by mitgedanken
Class and Interface
See also Module
Q&A
Module vs Class
- an instancible module is an alternative to class;
only one instancible module can be exist per file. - many classes can exist in one file if they have different names.
I prefer instancible modules because `module` is a known concept of Euphoria.
So this is an extension instead of something completly new. In addition, modules are more flexible.
You can also define an interface as well as many other things,
such as complex structures.
Instancible modules are more powerful and generic.
Class
Define
class Name -- private by default - accessible by this class, only! integer a = 0 constant B = {"blah", ' '} -- can be accessed by an class or module export sequence w = "Hello World" -- can be accessed from a class that extends this class. public integer b = 1.3 -- This routine must exist only once if its -- parameters are the same (or if none are defined). -- Must not be called! - it's private and not callable. --- -- The program code shown is similar to the standard implementation. --- new(integer number) self.a = number -- or a = number end new -- This routine must exist only once. -- Must not be called! - it's private and not callable. --- destroy() ... end destroy -- This routine must exist only once. -- The program code shown is similar to the standard implementation. -- Must not be called! - it's private and not callable. --- failed (integer errorcode) puts(STDERR, "Error; code " + errorcode) end failed ... --- -- as normal function x() ... end function procedure y() ... end procedure end class
Use
instance C = new Name() -- may be @Class C = ... ?! c.x() sequence b = c.w -- or c#w constant p = c.B -- or c##B => must throw an error ('Constant `B` is private')
Interface
Define
Interface's routines are public by default! And an error must be thrown if visibility is defined (private, export, public)
interface Animal function name() ... end function end interface
Use
with class
class Tiger is Animal ... end class
instance tiger = new Tiger() if tiger instanceof Animal then -- is true end if
with instancible module
module Tiger is Animal ...
- history, backlinks
- Last modified Nov 07, 2022 by mitgedanken